/*
 * Socket 0.0.1 - Socket类
 *
 * Copyright (c) 2008 Dalton (9wee.com)
 *
 *
 * $Date: 2008-12-17 $
 * $Rev: 01 $
 *
 --------------------------------------------------
 更新日志


 */

function Socket() {
	this.host = "192.168.0.7";
	this.port = "8888";
	this.loginKey;
	this.privateKey;
	this.sending = false;
	this.nickname;
	this.sendData = new Array();
}


Socket.prototype = {

	connect: function() {
		if (typeof window.document.socket.connect == 'function') {
			debug("连接服务器" + this.host + ":" + this.port + "，请稍候...");
			window.document.socket.SetVariable("host", this.host);
			window.document.socket.SetVariable("port", this.port);
			window.document.socket.connect();
		}

		else {
			window.setTimeout("gameSocket.connect()", 500);
		}
	},

	join: function(key) {
		debug('以' + this.nickname + '加入聊天中心，请稍候...');
		var data =  key + '|' + 'J|' + this.nickname + '|' + this.loginKey + '|0';
		window.document.socket.SetVariable("data", data);
		window.document.socket.send();
	},

	close: function() {
		window.document.socket.close();
	},

	send: function(data, key) {
		if (undefined != key) {
			data = this.privateKey + '|' + data + '|' + key;
			debug("---发送请求---" + data);
			window.document.socket.SetVariable("data", data);
			window.document.socket.send();
			return true;
		}

		else if (undefined == data) {
			if (this.sendData.length != 0) {
				data = this.sendData.shift();
				if (typeof this.sendHandle == "function") {
					this.sending = true;
					this.sendHandle(data);
				}
			}
			return false;
		}

		else {
			if (this.sendData.length != 0) {
				this.sendData.push(data);
			}
			if (typeof this.sendHandle == "function") {

				this.sending = true;
				this.sendHandle(data);
			}
			return false;

		}

	},

	onConnect: function(success) {
		if (success == true) {
			debug("连接服务器成功");
			window.document.socket.SetVariable("data", "G");
			window.document.socket.send();
		}
	},

	onClose: function() {
	},

	onData: function(data) {
		debug('---返回请求---' + data);
		arr = data.split('|');
		if (this.nickname == arr[2]) {
			this.privateKey = arr[0];
			this.send();
		}

		switch (arr[1]) {
		case 'J':
			if(arr[5] == '88') {
				var user = arr[4].split('_');
				debug("加入成功");

				//导入场景地图及地图事件
				loadJS("mapJs", "js/maps/" + arr[3].split("_")[1] + ".js");
				mygame.setEvent();
				//增加自己
				mygame.addUser(mygame.curUser, "./images/dalton.gif", 32, 32, parseInt(user[0]), parseInt(user[1]), 30, 120);
				mygame.sceneId = arr[3].split('_')[1];
				//载入其他用户
				this.send('P|' + arr[3] + '|X|' + arr[4]);
			}
			break;
		case 'G':
			this.join(arr[0]);
			break;
		case 'L':
			var signal = arr[3].split('_')[0];
			if (signal == 'U') {
				if (arr[5] == '88') {
					var user = arr[4].split('_');
					mygame.addUser(arr[2], "./images/dalton.gif", 32, 32, parseInt(user[0]), parseInt(user[1]), 30, 120);

				}
				else if (arr[4] == '99') {
					if (arr[2] != this.nickname)
						mygame.removeUser(arr[2]);
				}
				else {

					var result = arr[4].split(','), user;
					for (var i in result) {
						if (!!result[i]) {
							var user = result[i].split('_');
							mygame.addUser(user[0], "./images/dalton.gif", 32, 32, parseInt(user[1]), parseInt(user[2]), 30,120);
						}
					}
				}
			}
			break;
		case 'Z':
			var result = arr[4].split('_');
			if (arr[2] != this.nickname) {
				var path = window.astar.getPath(mygame.users[arr[2]].by, mygame.users[arr[2]].bx, result[1], result[0]);
				mygame.users[arr[2]].movePath(path);
			}
			break;
		case 'W':
			gameChat.showMessage(arr[2], convertHTML(arr[3]));
			break;
		default:

		}
	},

	onInit: function() {
	}
};